package com.hnjd.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.hnjd.entity.Goods;
import com.hnjd.entity.Tops;
import com.hnjd.entity.Types;
import com.hnjd.dao.TypeMapper;
import com.hnjd.service.IgoodsService;
import com.hnjd.service.ItopsService;
import com.hnjd.service.ItypeService;
import com.hnjd.util.PageUtil;
import com.hnjd.util.UploadUtil;

@Controller
@RequestMapping("admin")
public class GoodsController {

	@Autowired 
	private IgoodsService goodsService;

	@Autowired 
	private ItypeService ts;

	@Autowired
	private ItopsService tt;

	@RequestMapping("/goodList.do")
	public String findAllGoods(Model m,Integer curPage,Integer type) {
     System.out.println(type);
		if(curPage==null) {
			curPage=1;
		}
		int count=50;
		int pageSize=10;
        	 if(type==null||type==0) {
        			PageUtil page = new PageUtil(count, pageSize, curPage);
        			System.out.println(page);
        			List<Goods> list = goodsService.findAllGoods(page,type);
        			m.addAttribute("page", page);
        			List<Tops> top = tt.findAllTops();
        			System.out.println(list);
        			m.addAttribute("list", list);
        			m.addAttribute("top", top);
        			return "good_list.jsp";
        	 }else {
        		 PageUtil page = new PageUtil(count, pageSize, curPage);
        		 List<Goods> list = tt.findGoods(page);
        		 m.addAttribute("list", list);
        		 List<Tops> top = tt.findAllTops();
        			m.addAttribute("tops", top);
        			m.addAttribute("page", page);
        		 return "good_list.jsp";
        	 }
      
	}

	@RequestMapping("/goodDelete.do")
	public String goodDelete(Integer id) {

		goodsService.goodDelete(id);
		return "goodList.do";
	}
	@RequestMapping("/findGoodById.do")
	public String findGoodById(Integer id,HttpSession session) {

		Map<String, Object> map = goodsService.findGoodById(id);
		Goods good = (Goods) map.get("good");
		List<Types> list = (List<Types>) map.get("list");
		System.out.println(good);
		System.out.println(list);
		session.setAttribute("good", good);
		session.setAttribute("list", list);
		return "good_edit.jsp";
	}


	@RequestMapping("/goodUpdate.do")
	public String updateGoods(Goods good,Integer typeid,HttpSession session,MultipartFile upload,HttpServletRequest request) throws Exception, IOException {
		
		String cover = UploadUtil.upload(upload, request);
		good.setCover(cover);
    System.out.println(cover);
		good.setTypeId(typeid);
		System.out.println("good"+good);
		goodsService.updateGoods(good);

		return "goodList.do";
	}


	@RequestMapping("/findType.do")
	public String findType(Model m) {

		List<Types> list = ts.findAllTypes();
		System.out.println(list);
		m.addAttribute("list", list);
		return "good_add.jsp";
	}

	@RequestMapping("/goodAdd.do")
	public String goodAdd(Goods good,MultipartFile upload,HttpServletRequest request) throws IllegalStateException, IOException {
		String cover = UploadUtil.upload(upload, request);
		good.setCover(cover);
		good.setSales(0);
		goodsService.goodAdd(good);

		return "goodList.do";
	}
	
	
}
